/// Got Implementation from http://www.yoda.arachsys.com/csharp/singleton.html 
/// Fourth Version - thanks

using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Collections.Specialized;

namespace org.opencube
{
	public sealed class Logger {
		public enum OMSLOGLEVEL {
			INF0,
			VERBOSE,
			DEBUG
		}//OMSLOGLEVEL

		static readonly Logger logger = new Logger();
		private string m_logFileName = null;
		private int m_logLevel = 1;
 
		// Explicit static constructor to tell C# compiler
		// not to mark type as beforefieldinit
		static Logger() {
		}
		
		Logger() {
//			try (
			this.m_logFileName = ConfigurationSettings.AppSettings[ "logfile" ];
			string configAppend = ConfigurationSettings.AppSettings[ "logappend" ];
			bool logAppend = false;
			if ( configAppend != null ) {
				logAppend = Boolean.Parse( configAppend );
			}//if
			if ( !logAppend && this.IsReady() ) {
				StreamWriter temp = new StreamWriter( this.m_logFileName, false );
				temp.Close();
			}//if
//			Console.WriteLine( "Logger instanziert" );
		}//Logger
		
		public void Log( string text, int logLevel ) {
			if ( ! this.IsReady() || ( logLevel > this.m_logLevel ) ){
				return;
			}//if
			//this.m_logfile.WriteLine( text );
//			Console.WriteLine( text );
			using( StreamWriter logFile = new StreamWriter( this.m_logFileName, true ) ) {
				logFile.WriteLine( text );
			}//using
		}//Log

		public void SetLevel( int level ) {
			this.m_logLevel = level;
		}//SetLevel

		private bool IsReady() {
			return ( this.m_logFileName != null );
		}//IsReady


		public static Logger Instance {
			get {
				return logger;
			}
		}//Logger
	}//Logger
}
